﻿package com.teaphy.coroutinecrawler.channel

import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val channel = Channel<Int>()

    launch {
        // 每间隔1s发送一条数据
        for (i in 0.until(5)) {
            channel.send(i)
            delay(1000)
        }

        println("发送数据结束！")
        // 将Channel关闭
        channel.close()
    }

    for (value in channel) {
        println("接收到的数据为：$value")
    }
}